package com.neu.wangweili.servlet;

import com.neu.wangweili.service.W_StuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "StuLoginServlet", urlPatterns = {"/StuLoginServlet"})
public class StuLoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String stu_cookie=request.getParameter("stu_cookies");
        String stu_no=request.getParameter("no");
        String stu_pwd=request.getParameter("pwd");
        W_StuService WStuService =new W_StuService();
        Map<String,Object>map = new HashMap<>();

        map= WStuService.login(stu_no,stu_pwd);

        if (map==null){
            request.getRequestDispatcher("commons/fail.jsp").forward(request,response);
        }else{
            if ("1".equals(stu_cookie)) {
                Cookie cookie=new Cookie("stu_info",stu_no+":"+stu_pwd);
                cookie.setMaxAge(60*60*24*7);
                response.addCookie(cookie);
            }
            System.out.println(map.get(stu_no));
            HttpSession session=request.getSession();
            session.setAttribute("item",map);
            request.getRequestDispatcher("WEB-INF/Stu/stu_main.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
